﻿using famails.App_Code.source.bus;
using famails.App_Code.source.dto;
using famails.bus;
using famails.common;
using famails.dto;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace famails.BackEnd.Customer
{
    public partial class CustomerCareAutoList : System.Web.UI.Page
    {
        static UserLoginDTO userLogin = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            userLogin = getUserLogin();
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        private UserLoginDTO getUserLogin()
        {
            if (Session["us-login"] != null)
            {
                return (UserLoginDTO)Session["us-login"];
            }
            else Response.Redirect("~");
            return null;
        }
        private void LoadData()
        {
            try
            {

                CM_GroupBUS ulBus = new CM_GroupBUS();
                DataTable table = ulBus.GetAll2Order(userLogin.UserId);

                ddlGroup.DataTextField = "GroupName";
                ddlGroup.DataValueField = "GroupID";

                DataRow row = table.NewRow();
                row["GroupID"] = -100;
                row["GroupName"] = "Tất cả";
                table.Rows.InsertAt(row, 0);
                
                ddlGroup.DataSource = table;
                ddlGroup.DataBind();
            }
            catch (Exception ex)
            {

                pnSuccess.Visible = false;
                pnError.Visible = true;
                lblError.Text = " Đã xảy ra lỗi trong quá trình thực hiện. Vui lòng thử lại !<br/>" + ex.Message;
                Common.logs(userLogin.Username + "-CustomerCare-Group - LoadData", ex, Common.logType.error);
            }
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_CustomerCareAutoDTO> Search(string subject, int c_group, int p_group, string from, string to)
        {
            userLogin = (UserLoginDTO)HttpContext.Current.Session["us-login"];
            List<CM_CustomerCareAutoDTO> result = new List<CM_CustomerCareAutoDTO>();
            try
            {
                //string name = "", phone = "h", email = ""; int group = -1;
                CM_CustomerCareAutoBUS ctBUS = new CM_CustomerCareAutoBUS();
                DataTable CustomerCareBySelect = ctBUS.Search(subject, c_group, p_group, from, to, userLogin.UserId);
                CM_CustomerCareAutoDTO item = new CM_CustomerCareAutoDTO();
                foreach (DataRow dr in CustomerCareBySelect.Rows)
                {
                    item = new CM_CustomerCareAutoDTO();
                    item.CustomerCareAutoID = Convert.ToInt32(dr["CustomerCareAutoID"] + "");
                    item.CustomerGroupName = dr["CustomerGroupName"] + "";
                    item.ProductGroupName = dr["ProductGroupName"] + "";
                    item.Sender = dr["Sender"] + "";
                    item.Subject = dr["Subject"] + "";
                    if (dr["CreateDate"] + "" != "")
                        item.StartDate = Convert.ToDateTime(dr["CreateDate"] + "");
                    result.Add(item);
                }
            }
            catch (Exception ex)
            {
                Common.logs("CustomerCare - DeleteEmail", ex, Common.logType.error);
            }
            return result;
        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static void Delete(int id)
        {
            try
            {
                CM_CustomerCareAutoBUS ctBUS = new CM_CustomerCareAutoBUS();
                //if (!ctBUS.CheckBeforDelete(id))
                    ctBUS.Delete(id);
                //else throw new Exception("Khách hàng này đã mua hàng, bạn không thể xóa!");
            }
            catch (Exception ex)
            {
                Common.logs("CustomerCare - DeleteEmail", ex, Common.logType.error);
                throw ex;
            }
        }


        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static List<CM_ProductGroupDTO> LoadTree()
        {
            List<CM_ProductGroupDTO> result = new List<CM_ProductGroupDTO>();
            CM_ProductGroupDTO item = new CM_ProductGroupDTO();
            item.ProductGroupID = 0;
            item.ProductGroupName = "Tất cả";
            item.children = new List<CM_ProductGroupDTO>();
            result.Add(item);
            CM_ProductGroupBUS bus = new CM_ProductGroupBUS();
            DataTable source = bus.GetProductGroup(userLogin.UserId, 1);
            foreach (DataRow row in source.Rows)
            {
                CM_ProductGroupDTO dto = new CM_ProductGroupDTO();
                dto.ProductGroupName = row["ProductGroupName"] + "";
                dto.ProductGroupID = Convert.ToInt32(row["ProductGroupID"]);
                dto.CreateDate = Convert.ToDateTime(row["CreateDate"]);
                dto.children = GetChildGroup(dto.ProductGroupID, Convert.ToInt32(row["GroupStep"]) + 1);
                result.Add(dto);
            }

            return result;
        }
        static List<CM_ProductGroupDTO> GetChildGroup(int parentid, int groupstep)
        {
            CM_ProductGroupBUS bus = new CM_ProductGroupBUS();
            List<CM_ProductGroupDTO> result = new List<CM_ProductGroupDTO>();
            DataTable source = bus.GetChildGroup(parentid, groupstep, userLogin.UserId);
            foreach (DataRow row in source.Rows)
            {
                CM_ProductGroupDTO dto = new CM_ProductGroupDTO();
                dto.ProductGroupName = row["ProductGroupName"] + "";
                dto.ProductGroupID = Convert.ToInt32(row["ProductGroupID"]);
                dto.children = GetChildGroup(dto.ProductGroupID, Convert.ToInt32(row["GroupStep"]) + 1);
                result.Add(dto);
            }
            return result;
        }
    }
}